package com.openbravo.format;

import com.openbravo.basic.BasicException;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public abstract class Formats
{
   public static final Formats NULL = new FormatsNULL( );
   public static final Formats INT = new FormatsINT( );
   public static final Formats STRING = new FormatsSTRING( );
   public static final Formats DOUBLE = new FormatsDOUBLE( );
   public static final Formats CURRENCY = new FormatsCURRENCY( );
  public static final Formats PERCENT = new FormatsPERCENT( );
   public static final Formats BOOLEAN = new FormatsBOOLEAN( );
   public static final Formats TIMESTAMP = new FormatsTIMESTAMP( );
   public static final Formats DATE = new FormatsDATE( );
  public static final Formats TIME = new FormatsTIME( );
   public static final Formats BYTEA = new FormatsBYTEA();
  private static NumberFormat m_integerformat = NumberFormat.getIntegerInstance();
  private static NumberFormat m_doubleformat = NumberFormat.getNumberInstance();
  private static NumberFormat m_currencyformat = NumberFormat.getCurrencyInstance();
  private static NumberFormat m_percentformat = new DecimalFormat("#,##0.##%");
  private static DateFormat m_dateformat = DateFormat.getDateInstance();
  private static DateFormat m_timeformat = DateFormat.getTimeInstance();
  private static DateFormat m_datetimeformat = DateFormat.getDateTimeInstance();

  public static int getCurrencyDecimals()
  {
    return m_currencyformat.getMaximumFractionDigits();
  }

  public String formatValue(Object paramObject)
  {
    if (paramObject == null)
      return "";
    return formatValueInt(paramObject);
  }

  public Object parseValue(String paramString, Object paramObject)
    throws BasicException
  {
    if ((paramString == null) || ("".equals(paramString)))
      return paramObject;
    try
    {
      return parseValueInt(paramString);
    }
    catch (ParseException localParseException)
    {
    	throw new BasicException(localParseException.getMessage(), localParseException);
    }
   
  }

  public Object parseValue(String paramString)
    throws BasicException
  {
    return parseValue(paramString, null);
  }

  public static void setIntegerPattern(String paramString)
  {
    if ((paramString == null) || (paramString.equals("")))
      m_integerformat = NumberFormat.getIntegerInstance();
    else
      m_integerformat = new DecimalFormat(paramString);
  }

  public static void setDoublePattern(String paramString)
  {
    if ((paramString == null) || (paramString.equals("")))
      m_doubleformat = NumberFormat.getNumberInstance();
    else
      m_doubleformat = new DecimalFormat(paramString);
  }

  public static void setCurrencyPattern(String paramString)
  {
    if ((paramString == null) || (paramString.equals("")))
      m_currencyformat = NumberFormat.getCurrencyInstance();
    else
      m_currencyformat = new DecimalFormat(paramString);
  }

  public static void setPercentPattern(String paramString)
  {
    if ((paramString == null) || (paramString.equals("")))
      m_percentformat = new DecimalFormat("#,##0.##%");
    else
      m_percentformat = new DecimalFormat(paramString);
  }

  public static void setDatePattern(String paramString)
  {
    if ((paramString == null) || (paramString.equals("")))
      m_dateformat = DateFormat.getDateInstance();
    else
      m_dateformat = new SimpleDateFormat(paramString);
  }

  public static void setTimePattern(String paramString)
  {
    if ((paramString == null) || (paramString.equals("")))
      m_timeformat = DateFormat.getTimeInstance();
    else
      m_timeformat = new SimpleDateFormat(paramString);
  }

  public static void setDateTimePattern(String paramString)
  {
    if ((paramString == null) || (paramString.equals("")))
      m_datetimeformat = DateFormat.getDateTimeInstance();
    else
      m_datetimeformat = new SimpleDateFormat(paramString);
  }

  protected abstract String formatValueInt(Object paramObject);

  protected abstract Object parseValueInt(String paramString)
    throws ParseException;

  public abstract int getAlignment();

  private static final class FormatsBYTEA extends Formats
  {
    protected String formatValueInt(Object paramObject)
    {
      try
      {
        return new String((byte[])(byte[])paramObject, "UTF-8");
      }
      catch (UnsupportedEncodingException localUnsupportedEncodingException)
      {
      }
      return "";
    }

    protected Object parseValueInt(String paramString)
      throws ParseException
    {
      try
      {
        return paramString.getBytes("UTF-8");
      }
      catch (UnsupportedEncodingException localUnsupportedEncodingException)
      {
      }
      return new byte[0];
    }

    public int getAlignment()
    {
      return 10;
    }
  }

  private static final class FormatsTIME extends Formats
  {
    protected String formatValueInt(Object paramObject)
    {
      return Formats.m_timeformat.format((Date)paramObject);
    }

    protected Object parseValueInt(String paramString)
      throws ParseException
    {
      return Formats.m_timeformat.parse(paramString);
    }

    public int getAlignment()
    {
      return 0;
    }
  }

  private static final class FormatsDATE extends Formats
  {
    protected String formatValueInt(Object paramObject)
    {
      return Formats.m_dateformat.format((Date)paramObject);
    }

    protected Object parseValueInt(String paramString)
      throws ParseException
    {
      return Formats.m_dateformat.parse(paramString);
    }

    public int getAlignment()
    {
      return 0;
    }
  }

  private static final class FormatsTIMESTAMP extends Formats
  {
    protected String formatValueInt(Object paramObject)
    {
      return Formats.m_datetimeformat.format((Date)paramObject);
    }

    protected Object parseValueInt(String paramString)
      throws ParseException
    {
      try
      {
        return Formats.m_datetimeformat.parse(paramString);
      }
      catch (ParseException localParseException)
      {
      }
      return Formats.m_dateformat.parse(paramString);
    }

    public int getAlignment()
    {
      return 0;
    }
  }

  private static final class FormatsBOOLEAN extends Formats
  {
    protected String formatValueInt(Object paramObject)
    {
      return ((Boolean)paramObject).toString();
    }

    protected Object parseValueInt(String paramString)
      throws ParseException
    {
      return Boolean.valueOf(paramString);
    }

    public int getAlignment()
    {
      return 0;
    }
  }

  private static final class FormatsCURRENCY extends Formats
  {
    protected String formatValueInt(Object paramObject)
    {
      return Formats.m_currencyformat.format(DoubleUtils.fixDecimals((Number)paramObject));
    }

    protected Object parseValueInt(String paramString)
      throws ParseException
    {
      try
      {
        return new Double(Formats.m_currencyformat.parse(paramString).doubleValue());
      }
      catch (ParseException localParseException)
      {
      }
      return new Double(Formats.m_doubleformat.parse(paramString).doubleValue());
    }

    public int getAlignment()
    {
      return 4;
    }
  }

  private static final class FormatsPERCENT extends Formats
  {
    protected String formatValueInt(Object paramObject)
    {
      return Formats.m_percentformat.format(DoubleUtils.fixDecimals((Number)paramObject));
    }

    protected Object parseValueInt(String paramString)
      throws ParseException
    {
      try
      {
        return new Double(Formats.m_percentformat.parse(paramString).doubleValue());
      }
      catch (ParseException localParseException)
      {
      }
      return new Double(Formats.m_doubleformat.parse(paramString).doubleValue() / 100.0D);
    }

    public int getAlignment()
    {
      return 4;
    }
  }

  private static final class FormatsDOUBLE extends Formats
  {
    protected String formatValueInt(Object paramObject)
    {
      return Formats.m_doubleformat.format(DoubleUtils.fixDecimals((Number)paramObject));
    }

    protected Object parseValueInt(String paramString)
      throws ParseException
    {
      return new Double(Formats.m_doubleformat.parse(paramString).doubleValue());
    }

    public int getAlignment()
    {
      return 4;
    }
  }

  private static final class FormatsSTRING extends Formats
  {
    protected String formatValueInt(Object paramObject)
    {
      return (String)paramObject;
    }

    protected Object parseValueInt(String paramString)
      throws ParseException
    {
      return paramString;
    }

    public int getAlignment()
    {
      return 2;
    }
  }

  private static final class FormatsINT extends Formats
  {
    protected String formatValueInt(Object paramObject)
    {
      return Formats.m_integerformat.format(((Number)paramObject).longValue());
    }

    protected Object parseValueInt(String paramString)
      throws ParseException
    {
      return new Integer(Formats.m_integerformat.parse(paramString).intValue());
    }

    public int getAlignment()
    {
      return 4;
    }
  }

  private static final class FormatsNULL extends Formats
  {
    protected String formatValueInt(Object paramObject)
    {
      return null;
    }

    protected Object parseValueInt(String paramString)
      throws ParseException
    {
      return null;
    }

    public int getAlignment()
    {
      return 2;
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.format.Formats
 * JD-Core Version:    0.6.0
 */